package com.itheima.System;

import com.itheima.damain.Student;

import java.util.ArrayList;
import java.util.Scanner;

public class ManagerSystem {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        ArrayList<Student> students = new ArrayList<>();

        System.out.println("---------欢迎登录学生管理系统---------");
        System.out.println("1:添加学生");
        System.out.println("2:删除学生");
        System.out.println("3:修改学生");
        System.out.println("4:查看学生");
        System.out.println("5:退出系统");
        System.out.println(">>>>>>>>>请选择您的操作：");


        loop:
        while (true) {

            String s = scanner.nextLine();

            switch (s) {
                case "1":
                    addStudent(students);
                    break;
                case "2":
                    deleteStudent(students);
                    break;
                case "3":
                    updateStudent(students);
                    break;
                case "4":
                    queryStudent(students);
                    break;
                case "5":
                    System.out.println("感谢使用！");
                    break loop;
                default:
                    System.out.println("您的输入有误，请重新输入！");
            }
        }


    }

    //判断学号是否存在
    private static int getIndex(ArrayList<Student> students, String strId) {

        int index = -1;

        for (int i = 0; i < students.size(); i++) {
            Student student = students.get(i);
            if (student.getId().equals(strId)) {
                index = i;
            }
        }
        return index;
    }

    //修改学生
    private static void updateStudent(ArrayList<Student> students) {

        Scanner sc = new Scanner(System.in);

        while (true) {

            System.out.println("请输入要修改的学生学号：");
            String strId = sc.nextLine();

            int index = getIndex(students, strId);

            if (index == -1) {
                System.out.println("学生信息不存在！");
            } else {
                System.out.println("请输入学生姓名：");
                String strName = sc.nextLine();

                System.out.println("请输入学生年龄：");
                String strAge = sc.nextLine();

                System.out.println("请输入学生出生日期：");
                String strBirthday = sc.nextLine();

                Student stu = new Student(strId, strName, strAge, strBirthday);
                students.set(index,stu);

                System.out.println("修改成功！");

                System.out.println("1:添加学生");
                System.out.println("2:删除学生");
                System.out.println("3:修改学生");
                System.out.println("4:查看学生");
                System.out.println("5:退出系统");
                System.out.println(">>>>>>>>>请选择您的操作：");

                return;
            }
        }
    }

    //删除学生
    private static void deleteStudent(ArrayList<Student> students) {

        Scanner sc = new Scanner(System.in);

        while (true) {

            System.out.println("请输入要删除的学生学号：");
            String strId = sc.nextLine();

            int index = getIndex(students, strId);

            if (index == -1) {
                System.out.println("学生信息不存在！");
            } else {
                students.remove(index);
                System.out.println("删除成功！");

                System.out.println("1:添加学生");
                System.out.println("2:删除学生");
                System.out.println("3:修改学生");
                System.out.println("4:查看学生");
                System.out.println("5:退出系统");
                System.out.println(">>>>>>>>>请选择您的操作：");

                return;
            }
        }
    }

    //查看学生
    private static void queryStudent(ArrayList<Student> students) {

        if (students.size() == 0) {
            System.out.println("系统当前还未录入学生信息！");
            return;
        }

        System.out.println("学号" + "\t\t" + "姓名" + "\t\t" + "年龄" + "\t\t" + "生日");

        for (int i = 0; i < students.size(); i++) {
            Student student = students.get(i);
            System.out.println(student.getId() + "\t\t" + student.getName() + "\t\t" + student.getAge() + "\t\t" + student.getBirthday());
        }

        System.out.println("1:添加学生");
        System.out.println("2:删除学生");
        System.out.println("3:修改学生");
        System.out.println("4:查看学生");
        System.out.println("5:退出系统");
        System.out.println(">>>>>>>>>请选择您的操作：");
    }

    //添加学生
    private static void addStudent(ArrayList<Student> students) {

        Scanner sc = new Scanner(System.in);
        String strId;
        while (true){
            System.out.println("请输入学生学号：");
            strId = sc.nextLine();

            int index = getIndex(students, strId);

            if (index==-1){
                break;
            }else {
                System.out.println("学生信息已存在，请重新输入！");
            }
        }

        System.out.println("请输入学生姓名：");
        String strName = sc.nextLine();

        System.out.println("请输入学生年龄：");
        String strAge = sc.nextLine();

        System.out.println("请输入学生出生日期：");
        String strBirthday = sc.nextLine();

        Student s = new Student(strId, strName, strAge, strBirthday);

        students.add(s);
        System.out.println("添加成功！");

        System.out.println("1:添加学生");
        System.out.println("2:删除学生");
        System.out.println("3:修改学生");
        System.out.println("4:查看学生");
        System.out.println("5:退出系统");
        System.out.println(">>>>>>>>>请选择您的操作：");
    }
}
